const { Sequelize, DataTypes } = require('sequelize');

// 数据库配置
const sequelize = new Sequelize('lingxi_system', 'root', '888mei525', {
  dialect: 'mysql',
  host: 'localhost'
});

// 定义销售订单项模型
const SalesOrderItem = sequelize.define('sales_order_item', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  orderId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    field: 'order_id'
  },
  productId: {
    type: DataTypes.STRING(100),
    allowNull: true,
    field: 'product_id'
  },
  productName: {
    type: DataTypes.STRING(200),
    allowNull: false,
    field: 'product_name'
  },
  specification: {
    type: DataTypes.STRING(200),
    allowNull: true
  },
  quantity: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  unit: {
    type: DataTypes.STRING(20),
    allowNull: true
  },
  unitPrice: {
    type: DataTypes.DECIMAL(12, 2),
    allowNull: false,
    field: 'unit_price'
  },
  amount: {
    type: DataTypes.DECIMAL(12, 2),
    allowNull: false
  },
  remark: {
    type: DataTypes.STRING(200),
    allowNull: true
  }
}, {
  tableName: 'sales_order_items',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

async function checkSalesOrderItems() {
  try {
    // 连接数据库
    await sequelize.authenticate();
    console.log('✅ 数据库连接成功');
    
    // 查询最新的订单项记录
    const items = await SalesOrderItem.findAll({
      order: [['created_at', 'DESC']],
      limit: 10
    });
    
    console.log('📋 最新的订单项记录:');
    items.forEach((item, index) => {
      console.log(`订单项 ${index + 1}:`);
      console.log(`  ID: ${item.id}`);
      console.log(`  订单ID: ${item.orderId}`);
      console.log(`  产品ID: ${item.productId}`);
      console.log(`  产品名称: ${item.productName}`);
      console.log(`  规格: ${item.specification}`);
      console.log(`  数量: ${item.quantity}`);
      console.log(`  单价: ${item.unitPrice}`);
      console.log(`  金额: ${item.amount}`);
      console.log(`  创建时间: ${item.created_at}`);
      console.log('');
    });
    
    console.log(`📊 订单项总数量: ${items.length}`);
  } catch (err) {
    console.error('❌ 查询失败:', err);
  } finally {
    await sequelize.close();
  }
}

// 运行检查
checkSalesOrderItems();const { Sequelize, DataTypes } = require('sequelize');

// 数据库配置
const sequelize = new Sequelize('lingxi_system', 'root', '888mei525', {
  dialect: 'mysql',
  host: 'localhost'
});

// 定义销售订单项模型
const SalesOrderItem = sequelize.define('sales_order_item', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  orderId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    field: 'order_id'
  },
  productId: {
    type: DataTypes.STRING(100),
    allowNull: true,
    field: 'product_id'
  },
  productName: {
    type: DataTypes.STRING(200),
    allowNull: false,
    field: 'product_name'
  },
  specification: {
    type: DataTypes.STRING(200),
    allowNull: true
  },
  quantity: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  unit: {
    type: DataTypes.STRING(20),
    allowNull: true
  },
  unitPrice: {
    type: DataTypes.DECIMAL(12, 2),
    allowNull: false,
    field: 'unit_price'
  },
  amount: {
    type: DataTypes.DECIMAL(12, 2),
    allowNull: false
  },
  remark: {
    type: DataTypes.STRING(200),
    allowNull: true
  }
}, {
  tableName: 'sales_order_items',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

async function checkSalesOrderItems() {
  try {
    // 连接数据库
    await sequelize.authenticate();
    console.log('✅ 数据库连接成功');
    
    // 查询最新的订单项记录
    const items = await SalesOrderItem.findAll({
      order: [['created_at', 'DESC']],
      limit: 10
    });
    
    console.log('📋 最新的订单项记录:');
    items.forEach((item, index) => {
      console.log(`订单项 ${index + 1}:`);
      console.log(`  ID: ${item.id}`);
      console.log(`  订单ID: ${item.orderId}`);
      console.log(`  产品ID: ${item.productId}`);
      console.log(`  产品名称: ${item.productName}`);
      console.log(`  规格: ${item.specification}`);
      console.log(`  数量: ${item.quantity}`);
      console.log(`  单价: ${item.unitPrice}`);
      console.log(`  金额: ${item.amount}`);
      console.log(`  创建时间: ${item.created_at}`);
      console.log('');
    });
    
    console.log(`📊 订单项总数量: ${items.length}`);
  } catch (err) {
    console.error('❌ 查询失败:', err);
  } finally {
    await sequelize.close();
  }
}

// 运行检查
checkSalesOrderItems();